/**
 * 
 */
package com.app.service.impl.competenceserivce;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.app.dao.ICompetenceDao;
import com.app.service.BaseService;
import com.app.service.ICompetenceService;
import com.app.service.vo.CornCompetenceVo;

/**
 * @author Administrator
 *
 */
public class CompetenceServiceImpl extends BaseService implements ICompetenceService {

	/**
	 * 
	 */
	public CompetenceServiceImpl() {
		// TODO Auto-generated constructor stub
		logger.info(this.getClass()+" loader");
	}

	private static Logger logger = Logger.getLogger(CompetenceServiceImpl.class);
	

	private ICompetenceDao competencedao;
	
	@SuppressWarnings("unchecked")
	public void init()
	{
		logger.info("初始化权限数据放入缓存");
		List<CornCompetenceVo> volist = this.competencedao.getAllComptence();
		Map<String,CornCompetenceVo> map = new HashMap<String,CornCompetenceVo>();
		Map<String,CornCompetenceVo> idmap = new HashMap<String,CornCompetenceVo>();
		for(CornCompetenceVo vo:volist)
		{
			map.put(vo.getName(), vo);
			idmap.put(vo.getId(), vo);
		}
		this.getCacheService().saveObject("competence-list", volist);
		this.getCacheService().saveObject("competence-map", map);
		this.getCacheService().saveObject("competence-map-id", idmap);
	}
	
	/* (non-Javadoc)
	 * @see com.app.service.ICompetenceService#getAllCornCompetenceVo()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<CornCompetenceVo> getAllCornCompetenceVo() {
		// TODO Auto-generated method stub
		logger.info("获取所有权限对象");
		List<CornCompetenceVo> volist = ((List<CornCompetenceVo>) this.getCacheService().getObject("competence-list"));
		if(null==volist)
		{
			volist = this.competencedao.getAllComptence();
		}
		return volist;
	}

	/* (non-Javadoc)
	 * @see com.app.service.ICompetenceService#addCornCompetenceVo(com.app.service.vo.CornCompetenceVo)
	 */
	@Override
	public void addCornCompetenceVo(CornCompetenceVo vo) {
		// TODO Auto-generated method stub
		logger.info("增加权限对象");
		this.competencedao.addCompetence(vo);
		this.init();
	}

	/* (non-Javadoc)
	 * @see com.app.service.ICompetenceService#updateCornCompetenceVo(com.app.service.vo.CornCompetenceVo)
	 */
	@Override
	public void updateCornCompetenceVo(CornCompetenceVo vo) {
		// TODO Auto-generated method stub
		logger.info("修改权限对象");
		this.competencedao.updateCompetence(vo);
		this.init();
	}

	/* (non-Javadoc)
	 * @see com.app.service.ICompetenceService#deleteCornCompetenceVo(com.app.service.vo.CornCompetenceVo)
	 */
	@Override
	public void deleteCornCompetenceVo(CornCompetenceVo vo) {
		// TODO Auto-generated method stub
		logger.info("删除权限对象");
		this.competencedao.deleteCompetence(vo);
		this.init();
	}

	/* (non-Javadoc)
	 * @see com.app.service.ICompetenceService#getCornCompetenceVoById(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public CornCompetenceVo getCornCompetenceVoById(String id) {
		// TODO Auto-generated method stub
		logger.info("查询权限对象 id="+id);
		Map<String,CornCompetenceVo> idmap = (Map<String, CornCompetenceVo>) this.getCacheService().getObject("competence-map-id");
		return idmap.get(id);
	}

	

	public ICompetenceDao getCompetencedao() {
		return competencedao;
	}

	public void setCompetencedao(ICompetenceDao competencedao) {
		this.competencedao = competencedao;
	}

	
	
}
